<template>
  <!-- 文章评论列表 -->
  <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad" :error.sync="error"
    error-text="请求失败，点击重新加载">
    <!-- <van-cell v-for="item in list" :key="item.com_id+''" :title="item.content" /> -->
    <!-- 文章评论项 -->
    <CommentItem @click-reply="$emit('click-reply',$event)" :comment='item' v-for="item in list" :key="item.com_id+''">
    </CommentItem>
  </van-list>
</template>

<script>
// 文章评论项
import CommentItem from '@/components/comment-item'
// 获取评论或评论回复
import { getCommentsList } from '@/api/comments.js'
export default {
  data () {
    return {
      // 加载状态
      loading: false,
      // 是否全部加载
      finished: false,
      // 每页条数
      limit: 10,
      // 页码
      offset: null,
      // 评论条数
      totalCount: 0,
      // 错误显示
      error: false
    }
  },
  props: {
    // 评论类型
    type: {
      type: String,
      default: 'a'
    },
    // 文章id或评论id
    source: {
      type: [Object, Number],
      required: true
    },
    // 父传子文章评论数据
    list: {
      type: Array,
      required: true
    }
  },
  methods: {
    // 更新列表数据
    async onLoad () {
      try {
        // 随机错误
        if (Math.random() > 0.5) {
          console.lg(123)
        }
        // 获取评论请求
        const { data: res } = await getCommentsList({
          limit: this.limit,
          offset: this.offset,
          type: this.type,
          source: this.source + ''
        })
        console.log(res)
        this.list.push(...res.data.results)
        this.totalCount = res.data.total_count
        this.$emit('upadteTotal', this.totalCount)
        this.loading = false
        if (this.list.length === this.totalCount) {
          this.finished = true
        } else {
          this.offset = res.data.last_id
        }
      } catch (err) {
        this.error = true
        this.loading = false
      }
    }
  },
  computed: {},
  watch: {},
  filters: {},
  components: {
    CommentItem
  },
  created () { },
  mounted () { }
}
</script>

<style scoped lang='less'>
</style>
